import 'package:common/smart_dialog/smart_dialog.dart';
import 'package:flutter/material.dart';
import 'package:flutter_screenutil/flutter_screenutil.dart';
import 'package:get/get.dart';

import 'public_btn_tem.dart';

/// description
typedef PublicAlertWidgetClickCakkBack = void Function(WindowBtn item);

/// 自定义气泡（吐司）的UI展示
class PublicAlertWidget extends StatelessWidget {
  final PublicAlertWidgetClickCakkBack? callBack;
  final TextAlign textAlign;
  final BtnClickWindow? windowItem;

  const PublicAlertWidget({
    super.key,
    this.textAlign = TextAlign.start,
    this.windowItem,
    this.callBack,
  });

  @override
  Widget build(BuildContext context) {
    return SingleChildScrollView(
      child: Container(
        alignment: Alignment.center,
        margin: EdgeInsets.symmetric(horizontal: 45.w),
        decoration: const BoxDecoration(color: Colors.white, borderRadius: BorderRadius.all(Radius.circular(16))),
        child: Stack(
          children: [
            Image.asset('assets/app/images/auth/background_pop_top_image_1.png'),
            Container(
              padding: EdgeInsets.only(left: 20.w, right: 20.w, top: 25.h, bottom: 20.h),
              child: Column(
                crossAxisAlignment: CrossAxisAlignment.center,
                mainAxisAlignment: MainAxisAlignment.start,
                children: [
                  Container(
                    padding: const EdgeInsets.only(top: 0, bottom: 5),
                    child: Text(windowItem?.main ?? "温馨提示".tr,
                        style: TextStyle(fontSize: 20.sp, color: const Color(0xFF000000), fontWeight: FontWeight.w600),
                        textAlign: TextAlign.center),
                  ),
                  Column(
                    crossAxisAlignment: CrossAxisAlignment.start,
                    mainAxisSize: MainAxisSize.min,
                    children: [
                      Container(
                        margin: const EdgeInsets.symmetric(vertical: 10),
                        alignment: Alignment.center,
                        child: RichText(
                            textAlign: textAlign,
                            text: TextSpan(
                              children: <TextSpan>[
                                TextSpan(
                                    text: '${windowItem?.desc} \n',
                                    style: TextStyle(
                                        fontSize: 14.sp,
                                        color: const Color(0xFF000000),
                                        fontWeight: FontWeight.w500,
                                        height: 1.5)),
                              ],
                            )),
                      ),
                      if (windowItem!.winno == 'twobtn') _buidTwoButton()
                      // Row(
                      //   mainAxisAlignment: MainAxisAlignment.spaceBetween,
                      //   children: [
                      //     if (cancemText != null) Expanded(child: leftButton()),
                      //     if (cancemText != null) const SizedBox(width: 14),
                      //     Expanded(child: rightButton()),
                      //   ],
                      // ),
                    ],
                  ),
                ],
              ),
            ),
          ],
        ),
      ),
    );
  }

  // Widget _buidOnwButton() {
  //   return rightButton();
  // }

  Widget _buidTwoButton() {
    List<Widget> list = [];
    for (WindowBtn element in windowItem!.btns ?? []) {
      if (element.btnKey == "left") {
        list.add(Expanded(child: leftButton(item: element)));
        list.add(const SizedBox(width: 14));
      } else if (element.btnKey == "middle") {
        list.add(Expanded(child: rightButton(item: element)));
      }
    }
    return Row(mainAxisAlignment: MainAxisAlignment.spaceBetween, children: list);
  }

  // Widget _buidThreeButton() {
  //   return Row(
  //     mainAxisAlignment: MainAxisAlignment.spaceBetween,
  //     children: [
  //       if (cancemText != null) Expanded(child: leftButton(item: )),
  //       if (cancemText != null) const SizedBox(width: 14),
  //       Expanded(child: rightButton()),
  //     ],
  //   );
  // }

  Widget leftButton({required WindowBtn item}) {
    return GestureDetector(
      onTap: () {
        SmartDialog.dismiss();
        if (item.btnData!.click!.react == 'close') {
        } else {
          callBack?.call(item);
        }
      },
      child: Container(
        height: 40.h,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            borderRadius: BorderRadius.all(Radius.circular(22.r)),
            border: Border.all(color: const Color(0xFF25D4D0), width: 1)),
        child: Text(item.btnTxt ?? "我知道了".tr, style: TextStyle(color: const Color(0xFF25D4D0), fontSize: 16.sp)),
      ),
    );
  }

  Widget rightButton({required WindowBtn item}) {
    return GestureDetector(
      onTap: () {
        SmartDialog.dismiss();
        if (item.btnData!.click!.react == 'close') {
        } else {
          callBack?.call(item);
        }
      },
      child: Container(
        height: 40.h,
        alignment: Alignment.center,
        decoration: BoxDecoration(
            color: const Color(0xFF25D4D0),
            borderRadius: BorderRadius.all(Radius.circular(22.r)),
            border: Border.all(color: const Color(0xFF25D4D0), width: 1)),
        child: Text(item.btnTxt ?? "我知道了".tr, style: TextStyle(color: const Color(0xFFFFFFFF), fontSize: 16.sp)),
      ),
    );
  }
}
